package base.aqs.countdownlatch;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author chengzhang
 * 2021/8/24 20:04
 */
public class CountDownLatch02 {


    private static CountDownLatch countDownLatch = new CountDownLatch(2);

    /**
     * CountDownLatch 与 join方法的区别
     */
    public static void main(String[] args) throws InterruptedException {

        Map<String,String> map = new HashMap();
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        //将线程池01添加到线程池
        executorService.submit(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                countDownLatch.countDown();
            }
            System.out.println("child thread01 over");
        });

        //将线程池02添加到线程池
        executorService.submit(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                countDownLatch.countDown();
            }
            System.out.println("child thread02 over");
        });


        System.out.println("wait all child thread over");
        //等待子线程执行完毕，返回
        countDownLatch.await();
        System.out.println("all child thread over");
    }
}
